<?php

namespace App\Console;

use App\Http\Controllers\AnchorController;
use App\Model\Category;
use App\Models\Live\Anchor;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
        Commands\cutServie::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {

        // 每5分钟自动爬取主播信息
        $path = env('ANCHOR_PATH', '/home/shx/code');
        $schedule->exec("node $path/fetch-lives/index.js")
            ->everyFiveMinutes()
            ->appendOutputTo("$path/lives.log");

//            ->after(function () {
//                $lives = DB::table('lives')->get();
//                $time = time();
//                DB::table('anchor_count')->insert($lives->map(function ($live) use($time) {
//                    return [
//                        'count' => Anchor::where('live_id', $live->id)->count(),
//                        'live_id' => $live->id,
//                        'time' => $time
//                    ];
//                })->all());
//                $controller = new AnchorController();
//                $p_cate = Category::where('cate_name', '直播')->first();
//                $cates = Category::where('cate_pid', $p_cate->cate_id)->orderBy('cate_weight','asc')->get()
//                    ->map(function ($cate) use($controller) {
//                        $cate->count = $controller->getCateCount($cate);
//                        return [
//                            'cate' => $cate,
//                            'items' => $controller->getCateAnchors($cate, 11),
//                        ];
//                    });
//
//                Cache::forget('lives.index');
//                Cache::forget('lives.rec');
//                Cache::forget('lives.top');
//                Cache::forever('lives.index',  $cates);
//                Cache::forever('lives.rec', $controller->getRecTop(6));
//                Cache::forever('lives.top', $controller->getLiveTop());
//            })

        $schedule->exec('pkill node')->everyThirtyMinutes();
    }

    /**
     * Register the Closure based commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        require base_path('routes/console.php');
    }
}
